using UnityEngine;
using UnityEngine.UI;
using QFramework;
using Unity.VisualScripting;
using QFramework.Example;

namespace ProjectSurvivor
{
	public class UIGamePanelData : UIPanelData
	{
	}
	public partial class UIGamePanel : UIPanel
	{
		public static EasyEvent FlashScreen=new EasyEvent();
		public static EasyEvent OpenTreasurePanel=new EasyEvent();
		protected override void OnInit(IUIData uiData = null)
		{
			mData = uiData as UIGamePanelData ?? new UIGamePanelData();
			// please add init code here

			// Global.HP.RegisterWithInitValue(hp =>
			// {
			// 	HpText.text = "Hp:" + Global.HP.Value + "/" + Global.MaxHp.Value;
			// }).UnRegisterWhenGameObjectDestroyed(gameObject);

			// Global.MaxHp.RegisterWithInitValue(hp =>
			// {
			// 	HpText.text = "Hp:" + Global.HP.Value + "/" + Global.MaxHp.Value;
			// }).UnRegisterWhenGameObjectDestroyed(gameObject);
			//敌人数量显示
			EnemyGenerator.EnemyCount.RegisterWithInitValue(enemyCount =>
			{
				EnemyCountText.text = "敌人:" + enemyCount;
			}).UnRegisterWhenGameObjectDestroyed(gameObject);
			//时间显示
			Global.CurrentSeconds.RegisterWithInitValue(currentSeconds =>
			{
				if (Time.frameCount % 30 == 0)
				{
					var currentSecondsInt = Mathf.FloorToInt(currentSeconds);
					var seconds = currentSecondsInt % 60;
					var minutes = currentSecondsInt / 60;
					TimeText.text = "时间:" + $"{minutes:00}:{seconds:00}";
				}
			}).UnRegisterWhenGameObjectDestroyed(gameObject);
			//经验显示
			// Global.Exp.RegisterWithInitValue(exp =>
			// {
			// 	ExpValue.fillAmount=exp/(float)Global.ExpToNextLevel();
			// 	ExpText.text = "经验:(" + exp + "/"  + Global.ExpToNextLevel() + ")";
			// });
			//等级显示
			Global.Level.RegisterWithInitValue(lv =>
			{
				LevelText.text = "等级:" + lv;
			}).UnRegisterWhenGameObjectDestroyed(gameObject);
			ExpUpgradePanel.Hide();
			//升级后暂停
			Global.Level.Register(lv =>
			{
				Time.timeScale = 0;
				ExpUpgradePanel.Show();
				AudioKit.PlaySound("LevelUp");
			}).UnRegisterWhenGameObjectDestroyed(gameObject);
			//
			Global.Exp.RegisterWithInitValue(exp =>
			{
				if (exp >= Global.ExpToNextLevel())
				{
					Global.Exp.Value -= Global.ExpToNextLevel();
					Global.Level.Value++;
				}
			}).UnRegisterWhenGameObjectDestroyed(gameObject);

			//查找含有敌人波次
			var enemyGenerator = FindObjectOfType<EnemyGenerator>();
			ActionKit.OnUpdate.Register(() =>
			{
				//当前描述
				Global.CurrentSeconds.Value += Time.deltaTime;
				//最后一波                      现在波数为空                           敌人数量为0
				if (enemyGenerator.LastWave && enemyGenerator.CurrentWave == null 
				&& EnemyGenerator.EnemyCount.Value == 0)
				{
					this.CloseSelf();
					//打开通关面板
					UIKit.OpenPanel<UIGamePassPanel>();
				}
			}).UnRegisterWhenGameObjectDestroyed(gameObject);

			Global.Coin.Value = PlayerPrefs.GetInt("coin", 0);
			Global.Coin.RegisterWithInitValue(coin =>
			{
				CoinText.text = "金币:" + coin;
			}).UnRegisterWhenGameObjectDestroyed(gameObject);
			 FlashScreen.Register(() =>
            {
                ActionKit
                    .Sequence()
                    .Lerp(0, 0.5f, 0.1f,
                        alpha => ScreenColor.ColorAlpha(alpha))
                    .Lerp(0.5f, 0, 0.2f,
                        alpha => ScreenColor.ColorAlpha(alpha),
                        () => ScreenColor.ColorAlpha(0))
                    .Start(this);
            }).UnRegisterWhenGameObjectDestroyed(gameObject);

			OpenTreasurePanel.Register(()=>
			{
				Time.timeScale=0.0f;
				TreasureChestPanel.Show();
			}).UnRegisterWhenGameObjectDestroyed(gameObject);

            // OpenTreasurePanel.Register(() =>
            // {
            //     Time.timeScale = 0.0f;
            //     TreasureChestPanel.Show();
            // }).UnRegisterWhenGameObjectDestroyed(gameObject);
		}

		protected override void OnOpen(IUIData uiData = null)
		{
		}

		protected override void OnShow()
		{
		}

		protected override void OnHide()
		{
		}

		protected override void OnClose()
		{
		}
	}
}
